package com.iplatform.iplatformuser.function.group.controller;

import com.iplatform.commons.pagination.Pagination;
import com.iplatform.iplatformuser.framework.util.UniqueIDGenerator;
import com.iplatform.module.privilege.group.PriGroupInfoBean;
import com.iplatform.iplatformuser.function.group.service.inter.IPriGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 *
 * @author yb
 *
 */
@RestController
@RequestMapping("/function/group")
@ApiIgnore
public class PriGroupController {

	@Autowired
	private IPriGroupService service;

	/**
	 * 查询权限表
	 * @param request
	 * @param formMap
	 * @param pagination
	 * @return
	 */
	@RequestMapping("query")
	public ModelAndView query(HttpServletRequest request, @RequestParam Map<String, String> formMap,
                              @ModelAttribute(value = "pagination") Pagination pagination) {
		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		List<PriGroupInfoBean>  resultList = service.queryGroupInfo();

		PageInfo<PriGroupInfoBean> pageInfo = new PageInfo<>(resultList);

		pagination.setTotalCount(pageInfo.getTotal());
		pagination.setTotalPageNum(pageInfo.getPages());

		ModelAndView modelAndView = new ModelAndView("function/group/list");
		modelAndView.addObject("resourceList", resultList);
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}


	/**
	 * 根据名字查询权限表
	 * @param request
	 * @param formMap
	 * @param pagination
	 * @return
	 */
	@RequestMapping("queryByName")
	public ModelAndView queryByName(HttpServletRequest request, @RequestParam Map<String, String> formMap,
                                    @ModelAttribute(value = "pagination") Pagination pagination) {
		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		List<PriGroupInfoBean>  resultList = service.queryGroupByName(formMap);

		PageInfo<PriGroupInfoBean> pageInfo = new PageInfo<>(resultList);

		pagination.setTotalCount(pageInfo.getTotal());
		pagination.setTotalPageNum(pageInfo.getPages());

		ModelAndView modelAndView = new ModelAndView("function/group/list");
		modelAndView.addObject("resourceList", resultList);
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}

	@RequestMapping("deletepage")
	public ModelAndView deletePage(@RequestParam Map<String, String> formMap) {
		ModelAndView modelAndView = new ModelAndView("common/delete");
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}

	/**
	 * 根据id删除权限组
	 * @param request
	 * @param formMap
	 * @param pagination
	 * @return
	 */
	@RequestMapping("delete")
	public ModelAndView delete(HttpServletRequest request, @RequestParam Map<String, String> formMap,
                               @ModelAttribute(value = "pagination") Pagination pagination) {

		service.deleteGroupById(formMap.get("deleteId"));
		PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
		List<PriGroupInfoBean>  resultList = service.queryGroupInfo();

		PageInfo<PriGroupInfoBean> pageInfo = new PageInfo<>(resultList);

		pagination.setTotalCount(pageInfo.getTotal());
		pagination.setTotalPageNum(pageInfo.getPages());

		ModelAndView modelAndView = new ModelAndView("function/group/list");
		modelAndView.addObject("resourceList", resultList);
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}

	/**
	 * 更新页面
	 * @param formMap
	 * @return
	 */
	@RequestMapping("updatepage")
	public ModelAndView updatePage(@RequestParam Map<String, String> formMap) {
		List<PriGroupInfoBean>  resultList = service.queryGroup(formMap);
		ModelAndView modelAndView = new ModelAndView("function/group/update");
		if (resultList != null && resultList.size() > 0) {
			modelAndView.addObject("resultMap", resultList.get(0));
		}
		return modelAndView;
	}

	/**
	 * 更新权限组
	 * @param formMap
	 * @param session
	 */
	@RequestMapping("update")
	@ResponseBody
	public void update(@RequestParam Map<String, String> formMap, HttpSession session) {
		/*PriManagerBean priManager = (PriManagerBean) session.getAttribute(PrivilageConstants.PRI_MANAGER);
		formMap.put("updatedBy", priManager.getUserCode());*/
		service.updateGroup(formMap);

		ModelAndView modelAndView = new ModelAndView("redirect:/function/group/query");
		modelAndView.addObject("formMap", formMap);
		modelAndView.addObject(formMap);
	}

	/**
	 * 添加页面
	 * @param formMap
	 * @return
	 */
	@RequestMapping("insertpage")
	public ModelAndView insertpage(@RequestParam Map<String, String> formMap) {
		ModelAndView modelAndView = new ModelAndView("function/group/add");
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}

	/**
	 * 添加权限组
	 * @param formMap
	 * @param session
	 */
	@RequestMapping("add")
	@ResponseBody
	public void add(@RequestParam Map<String, String> formMap, HttpSession session) {
/*		PriManagerBean priManager = (PriManagerBean) session.getAttribute(PrivilageConstants.PRI_MANAGER);
		formMap.put("createdBy", priManager.getUserCode());*/

		formMap.put("groupId",UniqueIDGenerator.getUUID());
		service.insertGroup(formMap);

		ModelAndView modelAndView = new ModelAndView("redirect:/function/group/query");
		modelAndView.addObject("formMap", formMap);
		modelAndView.addObject(formMap);
	}

	/**
	 * 删除多个权限组页面
	 * @param formMap
	 * @return
	 */
	@RequestMapping("batchDeletePage")
	public ModelAndView batchDeletePage(@RequestParam Map<String, String> formMap) {
		ModelAndView modelAndView = new ModelAndView("common/batch_delete");
		modelAndView.addObject("formMap", formMap);
		return modelAndView;
	}

	/**
	 * 根据id删除多个权限组
	 * @param deleteIds
	 */
	@RequestMapping("batchDelete")
	@ResponseBody
	public void batchDelete(String[] deleteIds) {
		service.batchDeleteGroup(deleteIds);
	}

}
